"""
一些通用的股票数据处理方法
"""
import glob
import os
import csv
from datetime import datetime
from itertools import islice
from collections import Counter
import pandas as pd
import shutil
def code_fix(code):
    if str(code)[:2] == '11':
        return 'sh' + str(code)
    if str(code)[:2] == '12':
        return 'sz' + str(code)
# 得到当前日期是否为股票交易日
def df_table(df, index):
    import prettytable as pt
    # 利用prettytable对输出结果进行美化,index为索引列名:df_table(df,'market')
    tb = pt.PrettyTable()
    df = df.reset_index(drop=True)
    tb.add_column(index, df.index)
    for col in df.columns.values:  # df.columns.values的意思是获取列的名称
        tb.add_column(col, df[col])
    print(tb)

def get_bond_pre(symbol):
    location = str(symbol)[0:2]
    char = None  # 初始化 char 变量
    if location == '11':
        char = 'sh'
    elif location == '12':
        char = 'sz'
    if char is None:
        raise ValueError(f"Unsupported symbol prefix: {location}")
    return char


def get_pre(symbol):
    symbol = str(symbol)
    if symbol[0] == '6':
        return "sh" + str(symbol)
    if symbol[0] == '0' or symbol[0] == '3':
        return "sz" + str(symbol)

def add_pre(code):
    if code[0] == '6':
        return 'sh' + code
    elif code[0] == '0' or code[0] == '3':
        return 'sz' + code

def add_tail(code):
    if code[0] == '6':
        return code + '.SH'
    elif code[0] == '0' or code[0] == '3':
        return code + '.SZ'

def code_convert(symbol):
    """
    输入数字格式的股票代码 返回完整代码
    # 0xxxxx 深市A股
    # 3xxxxx 深市科创板
    # 6xxxxx 沪市A股
    :param symbol:不完整的股票代码
    :return:完整股票代码  szxxxxxx shxxxxxx
    """
    symbol = str(symbol)
    if len(symbol) == 6:
        if symbol[0] == '6':
            return "sh" + symbol
        if symbol[0] == '3':
            return "sz" + symbol
        else:
            return "unknown code"
    else:
        append_count = 6 - len(symbol)
        str_append = "0" * append_count
        return "sz" + str_append + symbol


def get_file_earliest_time(file_name):
    """
    获取指定股票文件中起始时间点 由于默认升序 所以第一个即为最早时间
    :param file_name:
    :return: 返回当前文件的起始时间
    """
    f = csv.reader(open(file_name, 'r'))
    for item in islice(f, 1, 2):
        year, month, day = list(map(int, item[0].split('-')))
        return datetime(year, month, day)


def get_file_latest_time(file_name):
    """
    获取指定股票文件中起始时间点 由于默认升序 所以第一个即为最早时间
    :param file_name:
    :return: 返回当前文件的起始时间
    """
    f = pd.read_csv(file_name)
    year, month, day = list(map(int, f.iloc[-1]["date"].split('-')))

    return datetime(year, month, day)


def fill_csv(file_name, start,end,resutl_folder):
    """
    如果文件的起始时间点晚于传入的datetime,用0填充空白时间数据，为便于算法处理，将溢价率设置为1000，避免被选中
    :param file_name:处理的文件名
    :param datetime:目标时间点，需要将所有文件的起始时间统一为该时间
    :param resutl_folder:输出目录
    :return:

    example:fill_csv('../rate_final/sh110033-bond.csv',datetime(2011, 1, 1))


    """
    earliest_time = get_file_earliest_time(file_name)
    latest_time = get_file_latest_time(file_name)
    shutil.copyfile(file_name, resutl_folder + file_name[-12:-4] + '.csv')

    # 如果文件的起始时间晚于预定时间
    df = pd.read_csv(file_name)

    if start < earliest_time:
        # 需要填充
        zero = pd.DataFrame({"date": pd.date_range(start, earliest_time, closed='left'),
                             "open": 1,
                             "high": 1,
                             "low": 1,
                             "close": 1,
                             "volume": 1,
                             "premium_rate": 1000},
                            )
        df = zero.append(df)

    if latest_time < end:
        zero = pd.DataFrame({"date": pd.date_range(latest_time, end, closed='right'),
                                "open": 1,
                                "high": 1,
                                "low": 1,
                                "close": 1,
                                "volume": 1,
                                "premium_rate": 1000},
                            )
        df = df.append(zero)

    df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
    df.to_csv(resutl_folder + file_name[-12:-4] + '.csv', index=0)
    # else:
        # 直接拷贝即可
        # shutil.copyfile(file_name, resutl_folder + file_name[-12:-4] + '.csv')


def fill_all_csv(start_time, end_time, folder='../rate_final', folder_result='../rate/'):
    """
    迭代统一所有文件的起始时间
    :param start_time:目标其实时间
    :param folder:数据所在目录
    :param folder_result:输出路径
    :return:

    example:fill_all_csv(datetime(2018, 1, 1))

    """
    datafile_list = glob.glob(os.path.join(folder, '*'))  # 数据文件路径列表
    for file_name in datafile_list:
        fill_csv(file_name, start_time, end_time,folder_result)


def merge_bond_rate(symbol, bond_folder, rate_folder, result_folder):
    """
    合并可转债历史数据和历史溢价率数据
    根据code找到可转债文件和历史溢价率文件并进行合并
    :param symbol:可转债代码
    :param bond_folder:可转债历史数据
    :param rate_folder:溢价率历史数据
    :return:None

    example:
    merge_bond_rate('sh110033',bond_folder='../bond_org/',rate_folder='../rate_org/',result_folder='./')

    """
    # code_for_bond = code
    # code_for_rate = code[2:]

    bond_file_name = bond_folder + str(symbol) + '.csv'
    rate_file_name = rate_folder + str(symbol) + '.csv'
    if os.path.exists(bond_file_name) and os.path.exists(rate_file_name):
        result_file_name = result_folder + str(symbol) + '.csv'

        pd_bond = pd.read_csv(bond_file_name, index_col=0)
        pd_rate = pd.read_csv(rate_file_name)

        result = pd.merge(pd_bond, pd_rate, how='inner', on='date')
        result.to_csv(result_file_name, index=False)


def merge_all_bond_rate(csv_all='所有可转债.csv',
                        bond_folder='../all_bond/',
                        rate_folder='../all_rate/',
                        result_folder='../rate_final/'):
    """
    合并所有的可转债历史和转股溢价历史
    :param csv_all: 文件列表
    :param bond_folder:可转债历史所在目录
    :param rate_folder:转股溢价历史所在目录
    :param result_folder:最终结果所在目录
    :return:None
    """
    bond = pd.read_csv(csv_all, usecols=['债券代码', '交易场所'])
    for index in bond.index:
        code = bond.loc[index, '债券代码']
        char = bond.loc[index, '交易场所'][-2:].lower()
        symbol = char + str(code)
        merge_bond_rate(symbol, bond_folder, rate_folder, result_folder)


# 之前用于分析数据中datetime情况的方法
def get_earliest_time(folder_name='../rate_final'):
    datafile_list = glob.glob(os.path.join(folder_name, '*'))  # 数据文件路径列表
    date_earliest = datetime(2021, 8, 2, 0, 0)
    cnt = Counter()
    for file in datafile_list:
        f = csv.reader(open(file, 'r'))
        for item in islice(f, 1, 2):
            year, month, day = list(map(int, item[0].split('-')))
            year_list = [year]
            tmp = Counter(year_list)
            cnt+= tmp
            temp = datetime(year, month, day, 0, 0)
            if temp < date_earliest:
                date_earliest = temp
    print(cnt)


def fix_workday(filename, target_filename):
    bond = pd.read_csv(filename)
    for index in bond.index:
        year, month, day = list(map(int, bond.loc[index, 'date'].split('-')))
        if is_sleep_day(datetime(year, month, day)):
            print(datetime(year, month, day))
            print(index)
            bond = bond.drop(labels=index)
    bond.to_csv(target_filename, index=0)


def fix_all(folder='../rate'):
    datafile_list = glob.glob(os.path.join(folder, '*'))  # 数据文件路径列表
    for file_name in datafile_list:
        # print(file_name,file_name.replace("rate","rate_fix"))
        fix_workday(file_name,file_name.replace("rate","rate_fix"))
# fix_all()
# get_file_latest_time('/Users/bird/PycharmProjects/lhjy-project/data_tool-tools/rate_final/sh110003.csv')